<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="runId" stripe remoteSort remoteFilter>
				<el-table-column label="#" type="index" width="50"></el-table-column>
<!--				<el-table-column label="流水号" prop="id" width="150"></el-table-column>-->
<!--				<el-table-column label="流程标题" prop="flowTitle" width="150"></el-table-column>-->
				<el-table-column label="电子签名" align="center">
					<template v-slot="scope">
						<div v-if="scope.row.signatureUrl">
							<el-image
								style="width: 80px; height: 40px; cursor: pointer"
								:src="scope.row.signatureUrl"
								:preview-src-list="[scope.row.signatureUrl]"
								fit="contain"
								close-on-click-modal
								preview-teleported
								:z-index="9999"
							>
							</el-image>
						</div>
						<span v-else class="unsigned-text">未签署</span>
					</template>
				</el-table-column>
				<el-table-column label="申请人" prop="applicantName" width="150"></el-table-column>
<!--				<el-table-column label="部门" prop="deptName" width="150"></el-table-column>-->
				<el-table-column label="申请人部门" prop="applicantDeptName" width="150"></el-table-column>
				<el-table-column label="审批人" prop="approverName" width="150"></el-table-column>
				<el-table-column label="审批人部门" prop="approverDeptName" width="150"></el-table-column>
				<el-table-column label="出差开始时间" prop="bleave.d59871" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="出差结束时间" prop="bleave.d59872" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="出发地" prop="bleave.departure" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="出差地点/目的地" prop="bleave.destination" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="交通工具" prop="bleave.transport" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="外出类型" prop="bleave.travelType" width="150" :show-overflow-tooltip="true">
					<template #default="scope">
							{{scope.row.bleave.travelType === undefined ? '--' :   scope.row.bleave.travelType == 1 ? '单程' : '往返'}}
					</template>
				</el-table-column>
				<el-table-column label="备注" prop="bleave.reason" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="状态" prop="stats" width="150">
					<template #default="scope">
						{{
							scope.row.stats === undefined ? '--' :
								scope.row.stats == '0' ? '审核中' :
									scope.row.stats == '1' ? '通过' :
										scope.row.stats == '2' ? '不通过' : '--'
						}}
					</template>
				</el-table-column>

				<!--				<el-table-column label="出差事由" prop="reason" width="300" :show-overflow-tooltip="true"></el-table-column>-->
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button-group v-if="scope.row.stats === '0'">
							<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">审批</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-dialog title="审批" v-model="dialogVisible" width="30%">
			<el-form :model="form" label-width="80px">
				<el-form-item label="审批结果">
					<el-radio-group v-model="form.stats">
						<el-radio label="1" >同意</el-radio>
						<el-radio label="2">拒绝</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="form.remark" type="textarea"></el-input>
				</el-form-item>
				<div>
					<spersonalFinance
						ref="signaturePad"
						:line-width="4"
						:line-color="'#409EFF'"
						@save="handleSave"
						@reset="reset"
						@error="handleError"
					></spersonalFinance>
				</div>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="submitForm">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</el-container>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'

import spersonalFinance from '@/components/scpersonalFinance/index.vue'
import { FINANCE_CODES } from '@/config/statusCodes'
export default {
	name: 'My Attend Record',
	components: {
		scSelectFilter,
		spersonalFinance
	},
	data() {
		return {
			dialog: {
				save: false,
				detailsSave:false
			},
			resultImg:'',
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				}
			],
			dialogVisible: false,
			form: {
				result: '',
				remark: ''
			},
			apiObj: null,
			search: {
				date:''
			}
		}
	},
	mounted() {
		this.initApiObj();
	},
	methods: {
		reset(){
			this.resultImg = ''
		},
		// 处理签名保存
		handleSave({ base64, file }) {
			// 自定义上传逻辑
			const formData = new FormData()
			formData.append('file', file)
			formData.append('code', FINANCE_CODES.BUSINESS_TRIP)

			this.$API.office.personalFinance.uploadFileByCode.post(formData)
				.then(res => {
					this.resultImg  = res.redirect
				})
				.catch(err => {
					this.$message.error('文件上传失败')
				})
		},
// 处理错误
		handleError(message) {
			this.$message.error(message)
		},
		// 初始化apiObj
		initApiObj() {
			const userInfo = this.$TOOL.data.get("USER_INFO");
			this.apiObj = {
				get: async (params) => {
					// 合并固定参数（userInfo.id）和动态参数（this.search）
					const mergedParams = {
						status: '202', //出差
						option2: userInfo.id,
						...this.search,
						...params // 允许外部传入的params覆盖默认值
					};
					return await this.$API.office.attend.approversQueryAttendanceData.get(mergedParams);
				}
			};
			// this.apiObj = {
			// 	get: async (params) => {
			// 		// 合并固定参数（userInfo.id）和动态参数（this.search）
			// 		const mergedParams = {
			// 			status: '202', //出差
			// 			option2: userInfo.id,
			// 			...this.search,
			// 			...params // 允许外部传入的params覆盖默认值
			// 		};
			// 		const res = await this.$API.office.attend.approversQueryAttendanceData.get(mergedParams);
			// 		if (res.code === 200) {
			// 			return {
			// 				total: res.data.total,
			// 				list: res.data.list.map(item => ({
			// 					...item,
			// 					bLeave: {
			// 						time: item.bleave.d59871,
			// 						endTime: item.bleave.d59872,
			// 						departure: item.bleave.departure,
			// 						destination: item.bleave.destination,
			// 						transport: item.bleave.transport,
			// 						travelType: item.bleave.travelType,
			// 						reason: item.bleave.reason
			// 					}
			// 				}))
			// 			};
			// 		} else {
			// 			return {
			// 				total: 0,
			// 				list: []
			// 			};
			// 		}
			// 	}
			// };
		},
		filterChange(data){
			this.search.date = data.date;
			this.$refs.table.upData(this.search); // 触发表格重新加载
		},
		// readBpmDetails(row,index)
		// {
		// 	this.$nextTick(() => {
		// 		this.$router.replace({
		// 			path: '/bpm/handle/read',
		// 			query: {
		// 				runId: row.runId
		// 			}
		// 		})
		// 	})
		// },
		readBpmDetails(row, index) {
			this.row = row;
			this.dialogVisible = true;
			// 清空表单数据
			this.form.result = '';
			this.form.remark = '';
		},
		submitForm() {
			if (
				typeof this.resultImg === 'undefined' ||
				this.resultImg === null ||
				(typeof this.resultImg === 'string' && this.resultImg.trim() === '')
			) {
				this.$message.error('请生成签名后或字迹规范后审批');
				return;
			}
			let params = {
				stats: this.form.stats,
				correlationId: this.row.correlationId +'',
				remark: this.form.remark,
				signatureUrl:this.resultImg
			}
			this.$API.system.attendConfig.bLeaveApprove.get(params).then(res => {
				this.initApiObj();
				// 处理表单提交逻辑
				console.log('审批结果:', this.form.result);
				console.log('备注:', this.form.remark);
				this.dialogVisible = false;
			})

		}
	}
}
</script>

<style>
</style>
